//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    uname: '',
    psw: '',
    verify: '',
    alert: '',
    verifyImgSrc: '',
    cookie: '',
    unameFocus: true,
    pswFocus: false,
    verifyFocus: false,
    firstLaunch: true
  },
  
  unameInputEnd: function(e) {
    this.setData({
      uname: e.detail.value
    })
  },
  pswInputEnd: function(e){
    this.setData({
      psw: e.detail.value
    })
  },
  verifyInputEnd: function(e){
    this.setData({
      verify: e.detail.value
    })
  },

  goLogin: function() {
    var checkResult = this.checkInput()
      if(!checkResult) {
        console.log("ok, post")
        wx.showToast({
          title: '请稍后',
          icon: 'loading',
          duration: 60000
        })
        var uname = this.data.uname
        var psw = this.data.psw
        var verify = this.data.verify
        var cookie = this.data.cookie
        console.log("uname="+uname+"&psw="+psw+"&verify="+verify+"&cookie="+cookie)
        var _this = this
        wx.request({
          url: 'https://hcdstudio.cn/icard/wx/ec2.php', 

          data: "uname="+uname+"&psw="+psw+"&verify="+verify+"&cookie="+cookie,

          header: {
              "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
          },
          method: "POST",
          success: function(res) {
            
            console.log(res.data)
            var resData = res.data.split("|")
            if(resData.length > 1){
              //登陆成功
              wx.setStorageSync('loginName', uname)
              // wx.setStorageSync('loginPsw', psw)
              _this.loginSuccess(res.data)
            }else{
              wx.showModal({
                title: '无法验证登陆信息',
                content: '请检查登陆用户名和密码和验证码',
                showCancel: false,
                
              })
              _this.getCode()
            }
          },
          fail: function(){
            wx.showModal({
              title: '提示',
              content: '请求失败',
              showCancel: false,
              
            })
            _this.getCode()
          },
          complete: function(){
            wx.hideToast()
          }
        })
      }else{
        console.log(checkResult)
        // this.setData({
        //   alert: checkResult
        // })
        
        wx.showModal({
          title: '提示',
          content: checkResult,
          showCancel: false,
          
        })
      }
  },
  checkInput: function(){
    var uname = this.data.uname
    var psw = this.data.psw
    var verify = this.data.verify
    if(uname.length < 8) {
      this.setData({
        unameFocus: true,
        pswFocus: false,
        verifyFocus: false
      })
      return "用户名长度不足"
    }
    if(psw.length < 6) {
      this.setData({
        unameFocus: false,
        pswFocus: true,
        verifyFocus: false
      })
      return "密码长度不足"
    }
    if(verify.length < 4) {
      this.setData({
        unameFocus: false,
        pswFocus: false,
        verifyFocus: true
      })
      return "验证码长度不足"
    }
    return false
  },

  onLoad: function(){
    wx.setNavigationBarTitle({
      title: '登陆',
      success: function(res) {
        // success
      }
    })
    this.setData({
      cookie: wx.getStorageSync('ecardCookie')
    })
    app.globalData.ecardCookie = this.data.cookie

    if(this.data.cookie){
        var _this = this
        wx.request({
          url: 'https://hcdstudio.cn/icard/wx/getRoom.php',
          header: {
              "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
          },
          data: "cookie="+this.data.cookie,
          method: 'POST',
          success: function(res){
            // success
            console.log("cookie ready: "+res.data)
            var resData = res.data.split("|")
            if(resData.length > 1){
              // cookie有效
              console.log("cookie ok")
              _this.loginSuccess(res.data)
              wx.hideToast()
            }else{
              _this.getCookie()
            }
          },
          fail: function(res) {
            // fail
            _this.getCookie()
          },
          complete: function(res) {
            // complete
          }
        })
      }else{
        this.getCookie()
      }
  },

  onShow: function () {
    console.log('onLoad')

    wx.showToast({
      title: '正在载入',
      icon: 'loading',
      duration: 60000
    })
    this.setData({
      uname: wx.getStorageSync('loginName'),
      psw: '',
      verify: ''
    })

    if(this.data.uname == ''){
      this.setData({
        unameFocus: true
      })
    }else{
      this.setData({
        unameFocus: false,
        pswFocus: true
      })
    }

    if(!this.data.firstLaunch){
      this.getCode()
    }
    
    this.setData({
      firstLaunch: false
    })

    
  },

  getCookie: function(){
    var _this = this
    wx.request({
      url: 'https://hcdstudio.cn/icard/wx/getCookie.php',
      success: function(res){
        // success
        console.log(res)
        _this.setData({
          cookie: res.data
        })
        app.globalData.ecardCookie = res.data
        wx.setStorageSync('ecardCookie', res.data)
        _this.getCode()
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },

  getCode: function(){
    var _this = this
    // console.log("cookie:"+this.data.cookie)
    wx.downloadFile({
      url: "https://hcdstudio.cn/icard/wx/getECard2.php?cookie="+_this.data.cookie,
      type: 'image', // 下载资源的类型，用于客户端识别处理，有效值：image/audio/video
      //header: {
        //"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8;"
      //}, // 设置请求的 header
      //data: "cookie="+_this.data.cookie,
      success: function(res){
        // success
        console.log(res)
        _this.setData({
              verifyImgSrc: res.tempFilePath
        })
        
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
        wx.hideToast()
      }
    })
  },

  refreshCode: function(){
    this.setData({
      verify: ''
    })
    this.getCode()
  },
  loginSuccess: function(data){
    wx.reLaunch({
      url: '../balance/balance?data='+data,
      success: function(res){
        // success
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  },
  testBalance: function(){
    wx.navigateTo({
      url: '../balance/balance',
      success: function(res){
        // success
      },
      fail: function(res) {
        // fail
      },
      complete: function(res) {
        // complete
      }
    })
  }
})
